package logic

import (
	"CampusCloudAid/dao/mysql"
	"CampusCloudAid/models"
	"database/sql"
	"time"
)

// FindMoney 查看用户金额
func FindMoney(id int64) (m *models.AccountBalance, err error) {
	//根据id去表中用户的金额明细
	if m, err = mysql.FindMoneyByUid(id); err != nil {
		return nil, err
	}
	return
}

// Recharge 充值
func Recharge(recharge *models.Recharge) (err error) {
	recharge.RechargeTime =
		sql.NullTime{
			Time:  time.Now(),
			Valid: true,
		}

	if err = mysql.Recharge(recharge); err != nil {
		return err
	}
	return
}

// GiftCard 绑定礼品卡号
func GiftCard(giftCard *models.GiftCard) error {
	//获取详细额度
	m, err := mysql.FindMoneyByUid(giftCard.UserId)
	if err != nil {
		return err

	}
	amount, err := mysql.GiftCard(giftCard)
	if err != nil {
		return err
	}
	m.GiftCard += amount
	m.UserId = giftCard.UserId
	//将获取的礼品卡金额，插入到用户账户中
	if err = mysql.UpdateMoney(m); err != nil {
		return err
	}
	return err
}
